package com.wy.Service.impl;

import com.wy.Service.UserService;
import com.wy.dao.UserDao;
import com.wy.pojo.User;
import com.wy.vo.PageVo;
import com.wy.vo.UserDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service("userServer")
@Transactional
public class UserServerimpl extends BaseServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Override
    public User login(String name, String password) {
        return userDao.findByNameAndPassword(name,password);
    }

    @Override
    public List<User> searchAll() {
        return userDao.findAll();
    }

    @Override
    public void del(Integer id) {
        userDao.deleteById(id);
    }

    @Override
    public void add(User user) {
        userDao.save(user);
    }

    @Override
    public PageVo searchPage(Integer current, Integer pageSize, UserDto dto) {
        StringBuilder sbd=new StringBuilder("select r from User r  where 1=1 ");
        if(!StringUtils.isEmpty(dto.getName())){
            sbd.append(" and r.name like :name");
            dto.setName("%"+dto.getName()+"%");
        }
        if(!StringUtils.isEmpty(dto.getRoleid())){
            sbd.append(" and r.roleid = :roleid");
            dto.setRoleid(dto.getRoleid());
        }
        if(!StringUtils.isEmpty(dto.getState())){
            sbd.append(" and r.state = :state");
            dto.setState(dto.getState());
        }
        return super.searchPagination(current,pageSize,sbd.toString(),dto);
    }
}
